package com.example.administrator.a0021okhttpjson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.textView);
    }

    public void showJson(View v){

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okhttpclient =  new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://www.sina.com.cn/")
                        .build();
                try {
                    Response response = okhttpclient.newCall(request).execute();
                    String stringResponse = response.body().string();
                    showRespones(stringResponse);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    private void showRespones(final String s){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv.setText(s);
            }
        });
    }
}
